package com.nodecollege.cloud.controller.admin;

import com.nodecollege.cloud.common.annotation.AdminLoginAnnotation;
import com.nodecollege.cloud.common.annotation.ApiAnnotation;
import com.nodecollege.cloud.common.model.NCResult;
import com.nodecollege.cloud.common.model.QueryVO;
import com.nodecollege.cloud.common.model.ApiDataPower;
import com.nodecollege.cloud.common.model.po.UpmsUser;
import com.nodecollege.cloud.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 运维后端用户管理
 *
 * @author LC
 * @date 2019/6/13 16:12
 */
@RestController
@AdminLoginAnnotation
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService userService;

    /**
     * 获取所有用户信息
     *
     * @author LC
     * @date 16:37 2019/6/13
     * @Param []
     **/
    @ApiAnnotation(modularName = "运维后端用户管理", description = "获取所有用户信息")
    @PostMapping("/getUserList")
    public NCResult<UpmsUser> getUserList(@RequestBody QueryVO<UpmsUser> query) {
        // 设置数据权限
        return userService.list(query);
    }

    /**
     * 更新用户信息
     *
     * @param user
     * @return
     */
    @ApiAnnotation(modularName = "运维后端用户管理", description = "更新用户信息")
    @PostMapping("/update")
    public NCResult<UpmsUser> update(@RequestBody UpmsUser user) {
        ApiDataPower apiDataPower = new ApiDataPower();
        userService.updateUser(user, apiDataPower);
        return NCResult.ok();
    }

    /**
     * 删除用户信息
     *
     * @param user
     * @return
     */
    @ApiAnnotation(modularName = "运维后端用户管理", description = "删除用户信息")
    @PostMapping("/delete")
    public NCResult delete(@RequestBody UpmsUser user) {
        ApiDataPower apiDataPower = new ApiDataPower();
        userService.delUser(user.getUserId(), apiDataPower);
        return NCResult.ok();
    }


    /**
     * 重置密码
     *
     * @param user
     * @return
     */
    @ApiAnnotation(modularName = "运维后端用户管理", description = "重置密码")
    @PostMapping("/resetPwd")
    public NCResult resetPwd(@RequestBody UpmsUser user) {
        userService.resetPwd(user);
        return NCResult.ok();
    }

    @ApiAnnotation(modularName = "用户内部接口", description = "获取用户信息列表！", accessSource = 2)
    @PostMapping("/getUserListByQuery")
    public NCResult<UpmsUser> getUserListByQuery(@RequestBody QueryVO queryVO){
        return NCResult.ok(userService.getUserListByQuery(queryVO));
    }

    @ApiAnnotation(modularName = "用户内部接口", description = "根据用户id获取用户信息！", accessSource = 2)
    @PostMapping("/getUserByUserId")
    public NCResult<UpmsUser> getUserListByUserId(@RequestBody Long userId){
        return NCResult.ok(userService.getUserByUserId(userId));
    }
}
